package com.up.management.controller;

import com.up.common.annotation.Log;
import com.up.common.core.controller.BaseController;
import com.up.common.core.domain.AjaxResult;
import com.up.common.core.page.TableDataInfo;
import com.up.common.enums.BusinessType;
import com.up.common.utils.poi.ExcelUtil;
import com.up.management.domain.entity.Item;
import com.up.management.domain.entity.ItemCategory;
import com.up.management.domain.entity.ItemRegion;
import com.up.management.domain.dto.ItemWithDetailDTO;
import com.up.management.domain.vo.ItemVO;
import com.up.management.domain.vo.ItemDetailVO;
import com.up.management.domain.vo.ItemCategoryVO;
import com.up.management.domain.vo.ItemRegionVO;
import com.up.management.service.IItemCategoryService;
import com.up.management.service.IItemRegionService;
import com.up.management.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目管理Controller
 * 
 * @author cloud
 * @date 2025-09-18
 */
@RestController
@RequestMapping("/management/item")
public class ItemController extends BaseController
{
    @Autowired
    private IItemService itemService;

    @Autowired
    private IItemCategoryService itemCategoryService;

    @Autowired
    private IItemRegionService itemRegionService;

    /**
     * 查询项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('management:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(Item item)
    {
        startPage();
        List<ItemVO> list = itemService.selectItemVOList(item);
        return getDataTable(list);
    }

    /**
     * 导出项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('management:item:export')")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Item item)
    {
        List<Item> list = itemService.selectItemList(item);
        ExcelUtil<Item> util = new ExcelUtil<Item>(Item.class);
        util.exportExcel(response, list, "项目管理数据");
    }

    /**
     * 获取项目分类列表
     */
    @GetMapping("/categories")
    public AjaxResult getCategories()
    {
        List<ItemCategoryVO> list = itemCategoryService.selectItemCategoryVOList(new ItemCategory());
        return success(list);
    }

    /**
     * 获取项目地区列表
     */
    @GetMapping("/regions")
    public AjaxResult getRegions()
    {
        List<ItemRegionVO> list = itemRegionService.selectItemRegionVOList(new ItemRegion());
        return success(list);
    }

    /**
     * 获取项目管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('management:item:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(itemService.selectItemVOById(id));
    }

    /**
     * 获取项目详情
     */
    @PreAuthorize("@ss.hasPermi('management:item:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getDetail(@PathVariable("id") Long id)
    {
        return success(itemService.selectItemDetailVOById(id));
    }

    /**
     * 新增项目管理
     */
    @PreAuthorize("@ss.hasPermi('management:item:add')")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ItemWithDetailDTO itemWithDetail)
    {
        return toAjax(itemService.insertItemWithDetail(itemWithDetail));
    }

    /**
     * 修改项目管理
     */
    @PreAuthorize("@ss.hasPermi('management:item:edit')")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ItemWithDetailDTO itemWithDetail)
    {
        return toAjax(itemService.updateItemWithDetail(itemWithDetail));
    }

    /**
     * 删除项目管理
     */
    @PreAuthorize("@ss.hasPermi('management:item:remove')")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(itemService.deleteItemByIds(ids));
    }

}
